<?php 
require_once 'Class/Certificate.php';
require_once 'Modules/BaseModule.php';
require_once 'Core/Session.php';

/**
 * 
 * 用户凭证
 *
 */
class M_Certificate extends ModuleBase{
	
	/**
	 * 登录
	 * @param Certificate $Certificate
	 * @return NULL|Certificate
	 */
	public function login($Certificate=null){
		if($Certificate===null || !isset($Certificate->Name) || empty($Certificate->Name)){
			return null;
		}
		foreach($this->db->select('Certificate',array('Name'=>$Certificate->Name)) as $i){
			if(md5($Certificate->Password)!==$i['Password'] || intval($i['Status'])<=0){
				return null;
			}
			$o=new Certificate();
			$o->Id=$i['Id'];
			$o->Name=$i['Name'];
			$o->Password=$i['Password'];
			$o->Permissions=intval($i['Permissions']);
			$o->CreateDate=intval($i['CreateDate']);
			$o->ModifyDate=intval($i['ModifyDate']);
			$o->Status=intval($i['Status']);
			return $o;			
		}
		return null;
	}
	
	/**
	 * 得到唯一实例
	 * @param Certificate $Certificate
	 * @return NULL|Certificate
	 */
	public function single($Certificate=null){
		if($Certificate===null || !isset($Certificate->Id) || empty($Certificate->Id)){
			return null;
		}
		$o=$this->_dishread('Certificate', $Certificate->Id);
		if($o===null){
			foreach($this->db->select('Certificate',array('Id'=>$Certificate->Id)) as $i){
				$o=new Certificate();
				$o->Id=$i['Id'];
				$o->Name=$i['Name'];
				$o->Password=$i['Password'];
				$o->Permissions=intval($i['Permissions']);
				$o->CreateDate=intval($i['CreateDate']);
				$o->ModifyDate=intval($i['ModifyDate']);
				$o->Status=intval($i['Status']);
			}
			if($o!==null){
				$this->_dishsave('Certificate', $o->Id, $o);
			}
		}
		return $o;
	}
	
	/**
	 * 添加一凭证
	 * @param Certificate $Certificate
	 * @return NULL|Certificate
	 */
	public function insert($Certificate=null){
		if($Certificate===null || isset($Certificate->Name) || empty($Certificate->Name))
			return null;
		$o=new Certificate();
		$o->Id=$this->db->guid();
		$o->Password=md5($Certificate->Password);
		$o->CreateDate=time();
		$o->ModifyDate=time();
		$o->Status=1;
		if($this->db->insert('Certificate',$o->__modetoarray())>0){
			$this->_dishsave('Certificate', $o->Id, $o);
			$o->Password=$Certificate->Password;
		}else{
			$o->Id=null;
		}
		return $o;
	}
	
	/**
	 * 修改一凭证
	 * @param Certificate $Certificate
	 * @return int
	 */
	public function update($Certificate=null){
		if(!isset($Certificate)||$Certificate===null){
			return 0;
		}
		$Certificate->ModifyDate=time();
		$Certificate=new Certificate($Certificate);
		if(isset($Certificate->Password)&& !empty($Certificate->Password)){
			$Certificate->Password=md5($Certificate->Password);
			$arr=$Certificate->__modetoarray(array('Id','CreateDate'));
		}else{
			$arr=$Certificate->__modetoarray(array('Id','Password','CreateDate'));
		}
		if($this->db->update('Certificate',$arr,array('Id'=>$Certificate->Id))>0){
			$this->_dishsave('Certificate', $Certificate->Id, $Certificate);
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 删除某一用户凭证
	 * @param int $Certificate
	 * @return int;
	 */
	public function delete($Certificate=null){
		if(empty($Certificate) || !isset($Certificate->Id) || empty($Certificate->Id)){
			return 0;
		}
		//$this->_dishclear('Certificate', $Certificate->Id);
		//return $this->db->delete('Certificate',array('Id'=>$Certificate->Id));
		$o=$this->single($Certificate);
		$o->Status=0;
		if($o!==null){
			if($this->db->update('Certificate',array('Status'=>0),array('Id'=>$o->Id))>0){
				$this->_dishsave('Certificate', $o->Id, $o);
				return 1;	
			}
		}
		return 0;
	}
	
	/**
	 * 
	 * @param $Id;$Name;$Status;$MinCreate;$MaxCreate;
	 */
	public function arraylist($search=null){
		
		$li=array();
		$cond=array();
		if(isset($search->Id) && !empty($search->Id)){
			$cond['Id']=$search->Id;
		}
		if(isset($search->Name) && !empty($search->Name)){
			$cond['Name']=$search->Name;
		}
		if(isset($search->Status) && is_numeric($search->Status)){
			$cond['Status']=$search->Status;
		}
		if(isset($search->MinCreate) && is_numeric($search->MinCreate)){
			$cond[]=array('CreateDate','>=',$search->MinCreate);
		}
		if(isset($search->MaxCreate) && is_numeric($search->MaxCreate)){
			$cond[]=array('CreateDate','<=',$search->MaxCreate);
		}
		$PageIndex=0;$PageSize=20;
		if($search!==null){
			if(is_numeric($search->PageIndex)){
				$PageIndex=$search->PageIndex;
			}
			if(is_numeric($search->PageSize)){
				$PageSize=$search->PageSize;
			}
		}
		foreach($this->db->select('Certificate',$cond,'',array(intval($PageIndex),intval($PageSize))) as $i){
			$o=new Certificate();
			$o->Id=$i['Id'];
			$o->Name=$i['Name'];
			$o->Permissions=intval($i['Permissions']);
			$o->CreateDate=intval($i['CreateDate']);
			$o->ModifyDate=intval($i['ModifyDate']);
			$o->Status=intval($i['Status']);
			$li[]=$o;
		}
		return $li;
	}
	
	/**
	 * 
	 * @param Certificate $Certificate
	 * @return TRUE | FALSE
	 */
	public function changepassword($Certificate=null){
		if($Certificate===null || empty($Certificate) ||
			!isset($Certificate->Id) || empty($Certificate->Id) || 
			!isset($Certificate->Password) || empty($Certificate->Password) || 
			!isset($Certificate->NewPassowrd) || empty($Certificate->NewPassowrd)){
			return false;
		}
		if($this->db->update('Certificate',array('Password'=>md5($Certificate->NewPassword)),array('Id'=>$Certificate->Id,'Password'=>md5($Certificate->Password)))>0){
			$NewPassowrd=$Certificate->NewPassowrd;
			$Certificate=$this->single($Certificate);
			$Certificate->Password=md5($NewPassowrd);
			$this->_dishsave('Certificate', $Certificate->Id, $Certificate);
			return true;
		}
		return false;
	}
}
?>